﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using brjostagjof.Models;
using System.Diagnostics;

namespace brjostagjof.Controllers
{
    public class PracticalInformationController : Controller
    {
        DataRepository rep = new DataRepository();

        public ActionResult HagnytarUpplysingar()
        {
            IEnumerable<InformationCategory> result = rep.GetAllInformationCategories();
            return View(result);
        }

        [HttpGet]
        public ActionResult AboutUs()
        {
            var result = rep.GetAboutUs();
            return View(result);
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddPracticalInformation()
        {
            return View(new PracticalInformation());
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        public ActionResult AddPracticalInformation(FormCollection formData)
        {
            InformationBase inf = new InformationBase();
            inf.headline = formData["headline"];
            inf.context = formData["context"];
            inf.date = DateTime.Now;
            PracticalInformation pi = new PracticalInformation();
            pi.informationCategoryID = int.Parse(formData["category"]);
            rep.AddPracticalInformation(pi,inf);
            return RedirectToAction("HagnytarUpplysingar");
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddPracticalInformationCategory()
        {
            return View(new InformationCategory());
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult AddPracticalInformationCategory(FormCollection formData)
        {
            InformationCategory picat = new InformationCategory();
            picat.category = formData["category"];
            rep.AddPracticalInformationCategory(picat);
            return RedirectToAction("HagnytarUpplysingar");
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult EditAboutUs()
        {
            var result = rep.GetAboutUs();
            return View(result);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        public ActionResult EditAboutUs(PracticalInformation p)
        {
            if (ModelState.IsValid)
            {
                p.InformationBase.date = DateTime.Now;
                rep.UpdateAboutUs(p);

                return RedirectToAction("AboutUs");
            }
            else
            {
                return View(p);
            }
        }

        [HttpGet]
        public ActionResult GetPracticalInformationByID(int? ID)
        {

            if (ID != null)
            {
                var result = rep.GetPracticalInformationByID(ID.Value);

                if (result == null)
                {
                    return View("Not Found");
                }
                else
                {
                    return View(result);
                }
            }
            else
            {
                return View("Not Found");
            }
        }

        [HttpGet]
        public ActionResult GetPracticalInformationByCategoryID(int? ID)
        {
            if (ID != null)
            {
                InformationCategory ic = rep.GetInformationCategoryByID(ID.Value);
                IEnumerable<PracticalInformation> result = rep.GetPracticalInformationByCategory(ic);

                return View(result);
            }
            else
            {
                return View("Not Found");
            }
        }

        [HttpGet]
        [Authorize(Roles = "Administrators")]
        public ActionResult EditPracticalInformation(int ID)
        {
            PracticalInformation result = rep.GetPracticalInformationByID(ID);
            Debug.WriteLine(result.InformationBase.headline);
            return View(result);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        public ActionResult EditPracticalInformation(PracticalInformation infoCategory)
        {
            if (ModelState.IsValid)
            {
                infoCategory.InformationBase.date = DateTime.Now;
                rep.UpdatePracticalInformation(infoCategory);

                return RedirectToAction("HagnytarUpplysingar");
            }
            else
            {
                return View(infoCategory);
            }
        }

        [HttpGet]
        public static IEnumerable<InformationCategory> GetInformationCategories()
        {
            DataRepository rep = new DataRepository();
            return rep.GetAllInformationCategories();
        }
    }
}
